﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
  public  class SYS_SiftWordDAL
    {
        private string DbName;
        public SYS_SiftWordDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_SiftWord model)
        {
            serverhelper sqlserverhelper = new serverhelper("");
            string primaryKey = sqlserverhelper.CreateKeyID();
            string sql = string.Format(@"insert into SYS_SiftWord(
			SW_ID
          ,Siftword
          ,CreatePerson
          ,CreateTime)
			values ('{0}','{1}','{2}','{3}')", primaryKey, model.Siftword, model.CreatePerson, DateTime.Now.ToString());
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string swID)
        {
            string sql = string.Format(@" delete from SYS_SiftWord where SW_ID='{0}' ", swID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取筛除词表数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetSiftWord()
        {
            string sql = " SELECT SW_ID,Siftword  FROM SYS_SiftWord ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 查询是否存在筛除词
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public int GetSiftWordByWord(string word)
        {
            string sql = string.Format(@" SELECT count(1)  FROM SYS_SiftWord where Siftword='{0}' ", word);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            return   Convert.ToInt32(oBase.ExecuteScalar(oCmd).ToString());
        }

        /// <summary>
        /// 修改筛除词
        /// </summary>
        public bool UpdateName(SYS_SiftWord model)
        {
            string sql = string.Format(@" update SYS_SiftWord set Siftword='{0}',CreatePerson='{1}',CreateTime= GETDATE() where SW_ID='{2}' ", model.Siftword, model.CreatePerson, model.SW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
    }
}
